package com.europe.business.europebusiness.ui.utils;

import com.europe.business.europebusiness.ui.common.Commons;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetUtils {

    public static Retrofit getRetrofit(){
        return getRetrofit(null);
    }


    public static Retrofit getRetrofit(final String token){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if(null != token){
            builder.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {

                    Request original = chain.request();
                    Request.Builder requestBuilder = original.newBuilder()
                            .header("Cookie", "jeesite.session.id"+"="+token);//平台
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            });
        }
        builder.connectTimeout(60, TimeUnit.SECONDS).
                readTimeout(60, TimeUnit.SECONDS).
                writeTimeout(60, TimeUnit.SECONDS);
        OkHttpClient client = builder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Commons.SERVER_URL)
                .client(client)
                .build();
        return retrofit;
    }
}
